package org.tyq.mail.utils;

import org.tyq.mail.entity.Mail;
import sun.misc.BASE64Encoder;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 邮件发送
 * Created by tyq on 16/11/2.
 */
public class SendMail {


    /**
     * 创建只发送文本的邮件
     * @param session
     * @param mail
     */
    public final static MimeMessage createSimpleMail(Session session,Mail mail) throws Exception{
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指明发件人
        message.setFrom(new InternetAddress(mail.getSender()));
        //接收人
        String[] recordAddr = mail.getReceiver();
        Address[] addresses = new Address[recordAddr.length];
        for (int i = 0; i < recordAddr.length; i++) {
            addresses[i] = new InternetAddress(recordAddr[i]);
        }
        //指明接收人
        message.setRecipients(Message.RecipientType.TO,addresses);
        //邮件标题
        message.setSubject(mail.getHead());
        //邮件内容
        message.setContent(mail.getMessage(), "text/html;charset=UTF-8");
        return message;
    }

    /**
     * 创建正文带图片/存在附件的邮件
     * @param session
     * @param mail
     * @return
     */
    public final static MimeMessage createImageMail(Session session,Mail mail) throws Exception{
        //创建邮件
        MimeMessage message = new MimeMessage(session);
        //发件人
        message.setFrom(new InternetAddress((mail.getSender())));
        //接收人
        String[] recordAddr = mail.getReceiver();
        Address[] addresses = new Address[recordAddr.length];
        for (int i = 0; i < recordAddr.length; i++) {
            addresses[i] = new InternetAddress(recordAddr[i]);
        }
        //指明接收人
        message.setRecipients(Message.RecipientType.TO,addresses);
        //邮件标题
        message.setSubject(mail.getHead());
        //准备邮件正文数据
        MimeBodyPart text = new MimeBodyPart();
        text.setContent(mail.getMessage(),"text/html;charset=UTF-8");
        //描述数据关系
        MimeMultipart mmp = new MimeMultipart();
        mmp.addBodyPart(text);
        //准备图片数据
        List<Map<String, InputStream>> fileList = mail.getFileList();
        if(fileList != null && fileList.size() > 0){
            for (int i = 0; i < fileList.size(); i++) {
                Map.Entry<String, InputStream> entry = fileList.get(i).entrySet().iterator().next();
                MimeBodyPart image = new MimeBodyPart();
                DataSource dataSource = new ByteArrayDataSource(entry.getValue(),"application/octet-stream");
                DataHandler dh = new DataHandler(dataSource);
                //DataHandler dh = new DataHandler(new FileDataSource("/Users/tyq/Downloads/psb.jpg"));
                image.setDataHandler(dh);
                //加上这句将作为附件发送,否则将作为信件的文本内容(必须)
                image.setFileName(entry.getKey());
                mmp.addBodyPart(image);
            }
        }
        // 这句很重要，千万不要忘了
        mmp.setSubType("related");
        message.setContent(mmp);
        message.saveChanges();
        return message;
    }

}
